Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NavigationServer3D.get_closest_point_to_segment() with use_collision #92850

Conversation

permelin
Copy link
Contributor

@permelin permelin commented Jun 6, 2024

When calling NavigationServer3D.get_closest_point_to_segment() with the parameter use_collision set to true, it always returns (0, 0, 0).

The bug can be seen here, where closest_point_d is incorrectly set to the same value as d on the first line, which hinders the else if below it from ever being true.

const real_t d = closest_point_d = p_from.distance_to(inters);
if (use_collision == false) {
closest_point = inters;
use_collision = true;
closest_point_d = d;
} else if (closest_point_d > d) {
closest_point = inters;
closest_point_d = d;
}

It's obvious that closest_point_d and closest_point should always be set together, so I can't imagine what the intention behind that first line could have been.

(I find the way these lines are written to be confusing with unnecessary duplication, but I resisted the urge to refactor them.)

@Calinou Calinou added this to the 4.3 milestone Jun 6, 2024
@Calinou Calinou requested a review from a team June 6, 2024 22:02
Copy link
Contributor

@Scony Scony left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested but the change looks correct. I've checked with blame and it looks like the problem was there form the very beginning.

If you have some more time then I'd recommend adding a unit test covering that code path. There are unit tests for map already so it should be a matter of copy-pasting and modifying a bit.

@permelin permelin force-pushed the fix-navigationserver3d-get_closest_point_to_segment-use_collision branch from 060a62c to 9b191f6 Compare June 7, 2024 12:24
@permelin permelin requested a review from a team as a code owner June 7, 2024 12:24
@permelin
Copy link
Contributor Author

permelin commented Jun 7, 2024

I've checked with blame and it looks like the problem was there form the very beginning.

Well, if you had written tests instead of this TODO, the bug would have been found earlier. 😁

// TODO: Test map_get_closest_point_to_segment() with p_use_collision=true as well.

If you have some more time then I'd recommend adding a unit test covering that code path.

I've added a couple of tests. The subcase looks awkward, but I don't know what else to do.

@Scony
Copy link
Contributor

Scony commented Jun 7, 2024

Good point. Maybe I was hesitant about that test case because it was giving me the wrong results :)
Anyway, now the change looks perfect.

@akien-mga akien-mga changed the title Fix NavigationServer3D.get_closest_point_to_segment() with use_collision Fix NavigationServer3D.get_closest_point_to_segment() with use_collision Jun 7, 2024
@akien-mga akien-mga merged commit 19affb6 into godotengine:master Jun 7, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

@permelin permelin deleted the fix-navigationserver3d-get_closest_point_to_segment-use_collision branch June 7, 2024 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants